import os
def find_all_board_index(debugPath):
    idAdded = []
    with open(debugPath,encoding="utf-8") as f:
        while True:
            line = f.readline()
            if len(line) <= 0:
                break
            if "裁切机: 铡刀数据更新完成" in line:
                idFinded = line.split("-")[3].strip().split(" ")[0]
                if idFinded not in idAdded:
                    idAdded.append(idFinded)
    return idAdded


def find_cutter_info_by_id(id):
    """
    根据id编号获取cutter检测的一些信息
    :param id:
    :return:
    """
    with open(debugPath, encoding="utf-8") as f:
        resInfo = "{}".format(id)
        adjust = False
        while True:
            line = f.readline()
            if len(line) <= 0:
                break
            if id not in line:
                continue
            if "起点检测: 验证失败,进行角度调整" in line:
                beginAngleIndex = line.index("调整角度")
                beginAngleBeforeAdjust = line[beginAngleIndex:].split("=")[1].strip()
                adjust = True
                continue

            if "起点检测: 验证成功" in line:
                beginAngleIndex = line.index("角度")
                indexEnd = line.index(",板边")
                beginAdjust = line[beginAngleIndex:indexEnd].split("=")[1]
                if adjust:
                    resInfo = resInfo + " " * 5 + beginAngleBeforeAdjust
                    resInfo = resInfo + " " * 5 + beginAdjust
                else:
                    resInfo = resInfo + " " * 5 + beginAdjust
                    resInfo = resInfo + " " * 5 + "无调整"

            if "终点检测" in line and ("验证通过" in line):
                firstAngleIndex = line.index("前角:")
                indexEnd = line.index(",后角:")
                indexEnd2 = line.index(",第一块偏")
                indexEnd3 = line.index((",第二块偏:"))
                indexEnd4 = line.index(",总偏差")

                firstAngle = line[firstAngleIndex:indexEnd].split(":")[1]
                secondAngle = line[indexEnd:indexEnd2].split(":")[1]
                firstError = line[indexEnd2:indexEnd3].split(":")[1]
                secondError = line[indexEnd3:indexEnd4].split(":")[1]
                totalError = line[indexEnd4:].split(":")[1]
                resInfo = resInfo + " " * 5 + firstAngle
                resInfo = resInfo + " " * 5 + secondAngle
                resInfo = resInfo + " " * 5 + firstError
                resInfo = resInfo + " " * 5 + secondError
                resInfo = resInfo + " " * 5 + totalError
                return resInfo




if __name__ == '__main__':
    debugPath = r"D:\xizhen_a6\2021-12-01_xizhenA6_debug.txt"
    allId = find_all_board_index(debugPath)
    cutterInfoList = []
    for id in allId:
        cutterInfo = find_cutter_info_by_id(id)
        if cutterInfo is not None:
            cutterInfoList.append(cutterInfo)
    debugDir,fileName = os.path.split(debugPath)
    savePath = os.path.join(debugDir,fileName[:-4] + "_res.txt")
    with open(savePath,"w") as f:
        f.write("id编号" + " " * 5 + "调整前" + " " * 5 + "调整后" + " "*5 + "前角" + " "*5 + "后角" + " "*5 + "第一块" + " "*5 + "第二块" + " " * 5 + "总偏差")
        for data in cutterInfoList:
            f.write(data)















